#include "TFTest.h"
#include "TFEventListener.h"
#include <BDThread.h>
#include <iostream>

TFTest::TFTest(std::string name)
	: m_bStartFlag(false)
	, m_bShouldRun(false)
	, m_sTestName(name)
{

}


TFTest::~TFTest()
{
}

void TFTest::Run()
{
	if (!m_bShouldRun) {
		return;
	}
	NoticeTestStart();
	SetUp();
	m_bStartFlag = true;
	bool ret = true;
	while (m_bStartFlag && ret) {
		ret = TestBody();
		BDThread::Sleep(100);
	}
	TearDown();
	NoticeTestEnd();
}

bool TFTest::TestBody()
{
	return true;
}

void TFTest::SetUp()
{

}

void TFTest::TearDown()
{

}

void TFTest::AddEventListener(TFEventListener* listener)
{
	if (listener == NULL) {
		return;
	}
	m_vecListener.push_back(listener);
}

void TFTest::NoticeTestStart()
{
	BDVector<TFEventListener*>::iterator it = m_vecListener.begin();
	for (; it != m_vecListener.end(); ++it) {
		(*it)->OnTestStart(this);
	}
}

void TFTest::NoticeTestEnd()
{
	BDVector<TFEventListener*>::iterator it = m_vecListener.begin();
	for (; it != m_vecListener.end(); ++it) {
		(*it)->OnTestEnd(this);
	}
}

void TFTest::NoticeUpdateUI(unsigned int shapes, unsigned int data)
{
	BDVector<TFEventListener*>::iterator it = m_vecListener.begin();
	for (; it != m_vecListener.end(); ++it) {
		(*it)->OnUpdateUI(this, shapes, data);
	}
}

void TFTest::Skip()
{
	m_bStartFlag = false;
}

void TFTest::StartRecord(const std::string& path)
{
	if (m_Csv.is_open()) {
		return;
	}
	m_Csv.open(path.c_str());
	m_Csv.set_delimiter(',', "##");
}

void TFTest::WriteRecord(BDVector<std::string>& stringList)
{
	if (!m_Csv.is_open()) {
		return;
	}
	BDVector<std::string>::iterator it = stringList.begin();
	for (; it != stringList.end(); ++it) {
		m_Csv << (*it);
	}
	m_Csv << NEWLINE;
}

void TFTest::StopRecord()
{
	if (m_Csv.is_open()) {
		m_Csv.close();
	}
}